package cn.wolfcode.web.controller;

import cn.wolfcode.domain.*;
import cn.wolfcode.qo.CustomerQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/potentialList")
    @RequiresPermissions(value = {"customer:potentialList","潜在客户列表"},logical = Logical.OR)
    public String list(Model model,@ModelAttribute("qo") CustomerQueryObject qo){
        //设置qo里的状态为潜在客户
        qo.setStatus(Customer.STATUS_COMMON);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //根据角色编码查询拥有该角色的员工
        List<Employee> employees = employeeService.selectByRole("Market_Manager","Market");
        model.addAttribute("employees", employees);
        //查询客户的所有职业
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.selectBySystemDictionaryId("job");
        model.addAttribute("jobs", systemDictionaryItems);
        //查询客户的所有来源
        List<SystemDictionaryItem> systemDictionaryItems1 = systemDictionaryItemService.selectBySystemDictionaryId("source");
        model.addAttribute("sources", systemDictionaryItems1);

        //跟进页面的交流方式查询
        List<SystemDictionaryItem> communicationMethod = systemDictionaryItemService.selectBySystemDictionaryId("communicationMethod");
        model.addAttribute("ccts", communicationMethod);
        return "customer/potentialList";
    }


    @RequestMapping("/input")
    @RequiresPermissions(value = {"potentialList:input","潜在客户编辑"},logical = Logical.OR)
    public String input(Long id,Model model){
        if(id != null){
            Customer customer = customerService.selectByPrimaryKey(id);
            model.addAttribute("customer", customer);
        }
        return "customer/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"potentialList:saveOrUpdate","潜在客户新增或修改"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Model model,@ModelAttribute("qo") Customer customer,Long id){
        try {
            if(id != null){
                customerService.update(customer);
            }else {
                customerService.insert(customer);
            }
            return new JsonResult();
        } catch(Exception e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage(),false);
        }

    }


}
